﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 

namespace UDPServer
{
    /// <summary>
    /// 文件导入选项,存储在主配置文件中的SettingParam节点
    /// </summary>
    public class SystemParam
    {

        #region 导入参数
         
        public int tcpPort = 13011; 
        public string tcpIP = "127.0.0.1";
        public string tcpCmd = "GetCurSampleSta";
        public bool bTcpAutoSend = true;

        public string mqttIp = "con.jowoiot.com";
        public int mqttPort = 1883;
        public string mqttUsr = "jlm_admin";
        public string mqttPwd = "jlm_admin";
        public string mqttPath = "jowoiot/toServer/v2/v3qes1k2";
        public string mqttContent = "";
        public string mqttDevName = "qy_as001";

        public bool bSimulateData = false;
        public int iMqttInteval = 6000;
        public bool bMqttAutoConn = true;
        public bool bMqttAutoSend = true;

        public string cpuID = "";

        #endregion


        static SystemParam myparam;

        public static SystemParam Create()
        {
            if (myparam == null) myparam = new SystemParam(); 
            return myparam;
        }

        private SystemParam()
        {
            LoadParam();
        }

        public void LoadParam()
        {
            ParamBase dbBase = ParamBase.Create();
            tcpPort = dbBase.GetValue("SystemParam", "tcpPort", "13011", tcpPort);
            tcpIP = dbBase.GetValue("SystemParam", "tcpIP", "127.0.0.1", tcpIP);
            tcpCmd = dbBase.GetValue("SystemParam", "tcpCmd", "GetCurSampleSta", tcpCmd);
            bTcpAutoSend = dbBase.GetValue("SystemParam", "bTcpAutoSend", "1", bTcpAutoSend);

            mqttIp = dbBase.GetValue("SystemParam", "mqttIp", "con.jowoiot.com", mqttIp);
            mqttPort = dbBase.GetValue("SystemParam", "mqttPort", "1883", mqttPort);
            mqttUsr = dbBase.GetValue("SystemParam", "mqttUsr", "jlm_admin", mqttUsr);
            mqttPwd = dbBase.GetValue("SystemParam", "mqttPwd", "jlm_admin", mqttPwd);
            mqttPath = dbBase.GetValue("SystemParam", "mqttPath", "jowoiot/toServer/v2/v3qes1k2", mqttPath);

            bSimulateData = dbBase.GetValue("SystemParam", "bSimulateData", "0", bSimulateData);
            iMqttInteval = dbBase.GetValue("SystemParam", "iMqttInteval", "6000", iMqttInteval);
            bMqttAutoConn = dbBase.GetValue("SystemParam", "bMqttAutoConn", "1", bMqttAutoConn);
            bMqttAutoSend = dbBase.GetValue("SystemParam", "bMqttAutoSend", "1", bMqttAutoSend);
            mqttDevName = dbBase.GetValue("SystemParam", "mqttDevName", "qy_as001", mqttDevName);
            mqttContent = dbBase.GetValue("SystemParam", "mqttContent", "test, artno001, 黄色, 工序01, 弯管, 淘宝, 100, 360, 2022-01-19 16:47, 2022-04-21 16:47", mqttContent);
        }

        public void SaveParam()
        {
            ParamBase dbBase = ParamBase.Create();
            dbBase.SetValue("SystemParam", "tcpPort", tcpPort);
            dbBase.SetValue("SystemParam", "tcpIP", tcpIP);
            dbBase.SetValue("SystemParam", "tcpCmd", tcpCmd);
            dbBase.SetValue("SystemParam", "bTcpAutoSend", bTcpAutoSend);

            dbBase.SetValue("SystemParam", "mqttIp", mqttIp);
            dbBase.SetValue("SystemParam", "mqttPort", mqttPort);
            dbBase.SetValue("SystemParam", "mqttUsr", mqttUsr);
            dbBase.SetValue("SystemParam", "mqttPwd", mqttPwd);
            dbBase.SetValue("SystemParam", "mqttPath", mqttPath);
            dbBase.SetValue("SystemParam", "bSimulateData", bSimulateData);
            dbBase.SetValue("SystemParam", "iMqttInteval", iMqttInteval);
            dbBase.SetValue("SystemParam", "bMqttAutoConn", bMqttAutoConn);
            dbBase.SetValue("SystemParam", "bMqttAutoSend", bMqttAutoSend);
            dbBase.SetValue("SystemParam", "mqttDevName", mqttDevName);
            dbBase.SetValue("SystemParam", "mqttContent", mqttContent);

            dbBase.Save();
        }
    }
}
